package com.longteng.diamond.controller;


import com.longteng.diamond.domain.InterfaceMock;
import com.longteng.diamond.po.RequestResult;
import com.longteng.diamond.po.TablePage;
import com.longteng.diamond.service.InterfaceMockService;
import com.longteng.diamond.util.ValidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;


/**
 * Created by Administrator
 * DATE : 2018/8/5
 * TIME : 10:34
 * PROJECT : java-lesson
 * PACKAGE : com.longteng.lesson
 *
 * @author <a href="mailto:taki@qq.com">Administrator</a>
 */
@Controller
@Slf4j
@RequestMapping("/interfaceMockController")
public class InterfaceMockController {
    @Autowired
    InterfaceMockService interfaceMockService;
    @Autowired
    ValidUtil validUtil;
    @RequestMapping("/interfaceMockPage")
    public String openMockPage(){
        return "page/interfaceMock/interfaceMockPage";
    }

    @RequestMapping("/getInterfaceMock")
    public @ResponseBody
    TablePage getInterfaceMock(InterfaceMock interfaceMock) {
        TablePage tablePage = new TablePage();
        try {
            tablePage = interfaceMockService.getInterfaceMock(interfaceMock);
        } catch (Exception e) {
            log.error("获取用户错误:", e);
        }
        return tablePage;
    }

    @RequestMapping("/addInterfaceMock")
    public ModelAndView addInterfaceMock(@Valid InterfaceMock interfaceMock, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceMock.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceMock/editInterfaceMockPage");
            return modelAndView;
        }
        try {
            interfaceMockService.addInterfaceMock(interfaceMock);
        } catch (Exception e) {
            log.error("新增接口错误", e);
            modelAndView.setViewName("/interfaceMockController/editInterfaceMock");
            modelAndView.addObject("errorMsg", e.toString());
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/interfaceMockController/interfaceMockPage");
        return modelAndView;
    }


    @RequestMapping("/updateInterfaceMock")
    public ModelAndView updateInterfaceMock(@Valid InterfaceMock interfaceMock, BindingResult bindingResult) {
        ModelAndView modelAndView = new ModelAndView();
        if (!validUtil.checkParam(interfaceMock.getClass().getDeclaredFields(), bindingResult, modelAndView)) {
            modelAndView.setViewName("page/interfaceMock/editInterfaceMockPage");
            return modelAndView;
        }
        try {
            interfaceMockService.updateInterfaceMock(interfaceMock);
        } catch (Exception e) {
            log.error("更新接口错误", e);
        }
        modelAndView.setViewName("redirect:/interfaceMockController/interfaceMockPage");
        return modelAndView;
    }

    @RequestMapping("/editInterfaceMockPage")
    public ModelAndView editInterfaceMockPage(InterfaceMock interfaceMock) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/interfaceMock/editInterfaceMockPage");
        if (null != interfaceMock.getInterfaceMockId() && interfaceMock.getInterfaceMockId() > 0) {
            interfaceMock = interfaceMockService.findInterfaceMockById(interfaceMock.getInterfaceMockId());
        }
        modelAndView.addObject("interfaceMock", interfaceMock);
        return modelAndView;
    }

    @RequestMapping("/deleteInterfaceMock")
    public @ResponseBody
    RequestResult deleteInterfaceMock(Integer interfaceMockId) {
        RequestResult requestResult = new RequestResult();
        try {
            interfaceMockService.deleteInterfaceMockById(interfaceMockId);
            requestResult.setSuccess(true);
        } catch (Exception e) {
            requestResult.setSuccess(false);
            requestResult.setMsg("删除接口异常,请联系管理员");
            log.error("删除接口异常", e);
        }
        return requestResult;
    }
}
